/**
 * Created by qingteng.wang on 2017/1/5.
 */

const gulp = require('gulp');
const sass = require('gulp-ruby-sass');
const pug = require('gulp-pug');
const autoprefixer = require('gulp-autoprefixer');
const imagemin = require('gulp-imagemin');
const babel = require('gulp-babel');
const spritemith = require('gulp.spritesmith');
const connect = require('gulp-connect');
const filecopy = require('gulp-file-copy');
const tinify = require('gulp-tinify');
const tinypng = require('gulp-tinypng-compress');

gulp.task('tinypng', function () {
    gulp.src('src/img/**/*.{png,jpg,jpeg}')
        .pipe(tinypng({
            key: 'fGZa8q8IZDrQ_KRh92yhqxfKyhy43VCI',
            sigFile: 'images/.tinypng-sigs',
            log: true
        }))
        .pipe(gulp.dest('images'));
});

gulp.task('sass',() => {
    sass('src/scss/index.scss',{})
    .on('error',sass.logError)
    .pipe(gulp.dest('dist/css'))
});
gulp.task('view',() => {
    return gulp.src('views/*.pug')
        .pipe(pug({

        }))
        .pipe(gulp.dest(''))
});

gulp.task('imgmin', () =>
    gulp.src('src/img/*')
        .pipe(imagemin())
        .pipe(gulp.dest('images'))
);

gulp.task('tinify',() => {
    gulp.src('src/img/*')
        .pipe(tinify('fGZa8q8IZDrQ_KRh92yhqxfKyhy43VCI'))
        .pipe(gulp.dest('images'))
})


gulp.task('babel',() => {
    gulp.src('src/js/*')
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(gulp.dest('dist/js'))
});

gulp.task('sprite',() => {
    gulp.src('src/img/sprite/*.png')
        .pipe(spritemith({
            imgName: 'img/sprite.png',//保存合并后图片的地址
            cssName: 'css/sprite.css',//保存合并后对于css样式的地址
            padding:5,//合并时两个图片的间距
            //algorithm: 'binary-tree',//注释1
            //cssTemplate:"dist/css/handlebarsStr.css"//注释2
        }))
        .pipe(gulp.dest('src'));
});

gulp.task('autoprefixer',()=>{
    gulp.src('dist/css/*.css')
        .pipe(autoprefixer({
            browsers: ['last 3 versions', 'safari 5', 'opera 12.1', 'ios 5', 'ios 6', 'android 4', 'ie 8', 'ie 9', 'Firefox >= 20'],
            cascade: false
        }))
        .pipe(gulp.dest('dist/css'))
});

gulp.task('connect', function() {
    connect.server();
});

gulp.task('copy',()=>{
    gulp.src('dist')
        .pipe(filecopy('doc',{
            start: 'dist'
        }))
});